package workers;

import domain.ConcurrentContainerInterface;

/**
 * @author igordv
 *
 */
public class Consumer extends AbstractWorker {
	protected int m_amount;
	/**
	 * @param container
	 */
	public Consumer(ConcurrentContainerInterface<Integer> container, int amount) {
		super(container);
		m_amount = amount;
	}
	public Consumer(ConcurrentContainerInterface<Integer> stack, int amount, int delay) {
		super(stack, delay);
		m_amount = amount;
	}
	/* (non-Javadoc)
	 * @see AbstractWorker#description()
	 */
	@Override
	public String description() {
		return "Consumer";
	}

	@Override
	public void threadLogic() {
		int i=0;
		while(i<m_amount){
			Integer removed = m_container.remove();
			if (removed!=null){
				i++;
//				if (Math.random()>0.5) Thread.yield();
//				System.out.println(removed);
//				try {
//					Thread.sleep((long) (Math.random()*m_delayTimeInMillis));
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
			}
		}
		System.out.println("next element in the stack: "+ m_container.remove());
	}
}
